package com.mtw.bbs.bbsUser.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mtw.bbs.bbsUser.pojo.form.MenuForm;
import com.mtw.bbs.common.core.constant.ServiceConstant;
import com.mtw.bbs.userAdmin.pojo.vo.MenuVo;
import com.mtw.bbs.common.core.vo.Result;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.mtw.bbs.bbsUser.pojo.entity.Menu;
import com.mtw.bbs.bbsUser.service.MenuService;
import java.util.Arrays;
import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("admin/menu")
public class MenuController {
    
    private final MenuService menuService;




    @ApiOperation(value = "新增菜单", notes = "新增一个菜单")
    @PostMapping("/save")
    public Result add(@RequestBody MenuForm form) {

        boolean saved = menuService.addMenu(form);
        return saved?Result.success():Result.fail("新增失败");
    }


    @ApiOperation(value = "修改菜单", notes = "修改指定菜单信息")
    @PutMapping(value = "/update")
    public Result update(@RequestBody MenuForm form) {
        boolean updated = menuService.updateMenu(form);
        return updated?Result.success():Result.fail("修改失败");
    }


    @ApiOperation(value = "删除菜单", notes = "根据url的id来指定删除对象")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean deleted = menuService.deleteByIds(Arrays.asList(id));
        return deleted?Result.success():Result.fail("删除失败");
    }



    @ApiOperation(value = "删除菜单", notes = "根据ids来指定删除对象")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestBody String[] ids) {
        boolean deleted = menuService.deleteByIds(Arrays.asList(ids));
        return deleted?Result.success():Result.fail("删除失败");
    }




    @ApiOperation(value = "获取菜单", notes = "获取指定菜单信息")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {

        Menu menu = menuService.getById(id);
        if (menu == null){
            return Result.fail("数据不存在");
        }
        return Result.success(menu.toVo());
    }



    /**
     * 查询所有菜单及按钮
     */
    @ApiOperation(value = "查询所有菜单及按钮", notes = "查询所有菜单及按钮")
    @GetMapping("/list")
    public Result list(){

        LambdaQueryWrapper<Menu> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Menu::getSort);
        lqw.orderByDesc(Menu::getCreatedDate);
        List<MenuVo> pageList = menuService.getPageList(lqw);
        return Result.success(pageList);
    }





    /**
     * 查询所有启用的菜单及按钮
     */
    @ApiOperation(value = "查询所有启用的菜单及按钮", notes = "查询所有启用的菜单及按钮")
    @GetMapping("/getAll")
    public Result getAll(){

        LambdaQueryWrapper<Menu> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Menu::getSort);
        lqw.orderByDesc(Menu::getCreatedDate);
        lqw.eq(Menu::getStatus, ServiceConstant.STATUS_ENABLE);
        List<MenuVo> pageList = menuService.getPageList(lqw);
        return Result.success(pageList);
    }
    
    



}
